package com.ls.sort;

import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Random;

/**
 * @author : 梁苏
 * @date : 2023/3/27 15:07
 **/
public class FileSortTest {

    @Test
    public void objectWriterOneTest() throws IOException, IllegalAccessException {
        ByteBuf buf = new HeapByteBuf(1024);
        ObjectWriter<User> objectWriter = new ObjectWriter(User.class, buf, new File("obj.data"));
        User user = new User();
        user.setId(1); // 4
        user.setName("小明"); // 6
        user.setAge(22); // 4
        objectWriter.writeObject(user);
        objectWriter.flush();
    }

    @Test
    public void objectReaderTest() throws IOException, IllegalAccessException {
        ByteBuf buf2 = new HeapByteBuf(1024);
        ObjectReader<User> objectReader = new ObjectReader(User.class, buf2, new File("obj.data"));
        User rUser = objectReader.readObject();
        System.out.println(rUser);
    }

    @Test
    public void objectWriterManyTest() throws IOException, IllegalAccessException {
        ByteBuf buf = new HeapByteBuf(1024);
        ObjectWriter<User> objectWriter = new ObjectWriter(User.class, buf, new File("obj.data"));
        for(int i = 0; i < 100; i++) {
            User user = new User();
            user.setId(i); // 4
            user.setName("小明" + i); // 6
            user.setAge(i); // 4
            objectWriter.writeObject(user);
        }
        objectWriter.flush();
    }

    @Test
    public void objectReaderManyTest() throws IOException, IllegalAccessException {
        ByteBuf buf2 = new HeapByteBuf(1024);
        ObjectReader<User> objectReader = new ObjectReader(User.class, buf2, new File("obj.data"));
        for (int i = 0; i < 100; i++) {
            User rUser = objectReader.readObject();
            System.out.println(rUser);
        }
    }

    @Test
    public void fileData_test() throws IOException, IllegalAccessException {
        List<User> list = new ArrayList<>();
        for(int i = 0; i < 100; i++) {
            User user = new User();
            user.setId(i);
            user.setName("小明" + i);
            user.setAge(i);
            list.add(user);
        }

        FileData fileData = new FileData(new File("obj.data"), list.size(), list.get(0).getClass(), new HeapByteBuf(100));
        fileData.writeToFile(list);

        fileData.setFetchSize(10);
        while (fileData.hasNext()) {
            System.out.println(fileData.next());
        }
    }

    @Test
    public void fileSort_test() throws IOException, IllegalAccessException {
        Comparator<User> comparator = Comparator.comparingInt(User::getAge);
        FileSort<User> fileSort = new FileSort<>(comparator, 1000);

        Random r = new Random();
        for (int i = 0; i < 10000; i++) {
            User user = new User();
            user.setId(i);
            user.setAge(r.nextInt(10000));
            user.setName("用户" + i);
            fileSort.add(user);
        }

        fileSort.sort();
    }

    @Test
    public void fileSort2_test() throws IOException, IllegalAccessException {
        FileData fileData = new FileData(new File("temp_1.data"), 1000, User.class, new HeapByteBuf(1024));

        fileData.setFetchSize(100);
        while (fileData.hasNext()) {
            System.out.println(fileData.getNext());
            fileData.next();
        }
    }

}
